package model

import (
	"fmt"

	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

type User struct {
	ID       uint `gorm:"primary_key"`
	Name     string
	Password string
	Role     string
}

var DB *gorm.DB

func InitDB() int {
	var err error
	DB, err = gorm.Open(sqlite.Open("./projects.db"), &gorm.Config{})
	// 检查是否有错误
	if err != nil {
		fmt.Println("连接数据库失败：", err)
		return -1
	}
	// 打印成功信息
	fmt.Println("连接数据库成功")
	err = DB.AutoMigrate(&User{})
	if err != nil {
		panic("failed to migrate!")
	}
	tmpUser1 := User{ID: 1, Name: "admin", Password: "e10adc3949ba59abbe56e057f20f883e", Role: "admin"}
	tmpUser2 := User{ID: 2, Name: "user", Password: "e10adc3949ba59abbe56e057f20f883e", Role: "user"}
	DB.Create(&tmpUser1)
	DB.Create(&tmpUser2)
	return 0
}
